home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / Demos / Sandstorm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-27  |  10.0 KB  |  301 lines  |  [TEXT/KAHL]

  1. /*
  2. Sandstorm.c
  3. Copyright © 1989-1993 Denis Pelli
  4. This program shows a noise movie, a dynamic random checkerboard, at 66.7 Hz!
  5. The key routine, which shows each frame, is the VideoToolbox routine CopyBitsQuickly().
  6.  
  7. To get a true impression of white noise you need to compute lots of noise
  8. frames, so give it as much memory as you can, e.g. 3 MB. (Use the Finder Get
  9. Info command.) Sandstorm fills all the memory you give it with pre-computed
  10. noise, which does take some time. Each displayed frame is taken from a random
  11. offset within that noise.
  12.  
  13. Note: the amount of data that has to be transfered by CopyBitsQuickly() in each
  14. frame is proportional to window area and the bits/pixel of the display, as set by
  15. Control Panel Monitors. At 8 bits/pixel the maximum window that can be updated
  16. at 66 Hz is about 192x192. At 1 bit/pixel we can do at least 480x640.
  17.  
  18. Sandstorm--not CopyBitsQuickly--calls WaitNextEvent() before each
  19. frame, in order to be responsive to the mouse, and this results in occasional
  20. pauses as the Mac takes time out to do various chores.
  21.  
  22.  
  23. HISTORY:
  24. 11/23/88 dgp derived from truenoiseDemo2.c
  25. 4/7/89 dgp     I removed nearly all the hardware dependencies. The only remaining one
  26.             is the call to NewFieldTFB(). I don't know how to get rid of that, short of
  27.             writing a VBL task.
  28. 9/29/89 dgp I've nearly updated this to THINK C 4.0, but the call to TickCount, 
  29.             refers to the old profiler, and I don't have time to figure this out now.
  30. 10/28/89 dgp I replaced the call to NewFieldTFB() by a call to GDSetEntries() which
  31.             is a device-independent way to wait for the end of frame.
  32. 7/19/90 dgp Unfortunately the built-in video on the Mac IIci is buggy and doesn't
  33.             support the setEntries call.
  34. 10/17/90 dgp Yay. Finally removed last bugs! Replaced SlotToScreenDevice() by
  35.             AddressToScreenDevice() in order to make it compatible with built-in video
  36.             on Mac IIci, IIsi, and LC. Removed the setEntries call for compatibility
  37.             with Mac IIci built-in video.(Apple has acknowledged bug, but bug is
  38.             still present in System 6.07.) Fixed bug whereby resizing window on other
  39.             than the main screen left window blank until it was dragged again. 
  40.             Removed unused variables. Added zoom box.
  41. 10/18/90 dgp Tidied up. Now zooms onto screen with largest intersection, and zooms back
  42.             to original size. Uses as much memory as user allocates to program. 
  43.             Window now always confines itself to a single screen, but can be dragged
  44.             anywhere.
  45. 10/20/90 dgp Copied code from VideoTest to close window and quit on Command-W or
  46.             Command-. Prevented window title bar from being hidden behind the menu
  47.             bar.
  48. 7/22/91    dgp    Made compatible with MPW C. This required changing INT_MAX to SHRT_MAX.
  49.             However, performance is slow, since CopyBitsQuickly.c doesn't generate
  50.             hand-tuned assembly code when using MPW C.
  51. 8/6/91    dgp    Replaced randU() by RandFill(), which runs twice as fast.
  52. 8/24/91    dgp    Made compatible with THINK C 5.0.
  53. 7/20/92    dgp    tidied up the comments slightly. Confirmed compatibility with 32-bit
  54.             addressing.
  55. 8/27/92    dgp    replace SysEnvirons() by Gestalt()
  56. 1/13/93 dgp    changed defaults to maximize speed.
  57. */
  58. #include "VideoToolbox.h"
  59. #if THINK_C
  60.     #include <LoMem.h>        // MBarHeight
  61. #else
  62.     short MBarHeight : 0xBAA;
  63. #endif
  64. void main(void);
  65. Boolean ZoomedOut(WindowPtr w);
  66. #define RAISE_PRIORITY    1    /* Optional. Contrary to Apple's rules,    */
  67.                             /* but speeds up the display. */
  68. #define SCROLL_BAR 15        /* Standard width of scroll bar */
  69. #define WAIT_NEXT_EVENT 0
  70.  
  71. EventRecord theEvent;    /* global */
  72.  
  73. void Sandstorm(void);
  74.  
  75. void main(void)
  76. {
  77.     Require(gestalt8BitQD);
  78.     Sandstorm();
  79. }
  80. void Sandstorm()
  81. {
  82.     register size_t ui;
  83.     register short *bufferPtr;
  84.     int i;
  85.     static char string[40];
  86.     static PixMap sand;
  87.     Rect r,unzoomedRect,gdRect;
  88.     GDHandle device;
  89.     WindowPtr window,aWindow,oldPort;
  90.     PixMap **myPixMapHandle;
  91.     static BitMap buffer;
  92.     long bufferBytes;
  93.     size_t sandBytes;
  94.     Boolean done=0,update;
  95.     long reSize;
  96.     int windowEvent=inDesk;
  97.     int error;
  98.     Boolean zoomedOut;
  99.     WStateData *wStateData;
  100.     RgnHandle ignoreRgn;
  101.     int pixelSize;
  102.     long value;
  103.     
  104.     /* INITIALIZE QuickDraw */
  105.     MaxApplZone();                        /* Expand heap to the limit. */
  106.     InitGraf(&qd.thePort);
  107.     InitFonts();
  108.     InitWindows();
  109.     InitCursor();
  110.     GetPort(&oldPort);
  111.     device=GetMainDevice();
  112.  
  113.     /* open a window on the device */
  114.     ignoreRgn=NewRgn();
  115.     SetRect(&r,0,0,192+SCROLL_BAR,192+SCROLL_BAR);
  116.     CenterRectInRect(&r,&(*device)->gdRect);
  117.     window=NewCWindow(NULL
  118.         ,&r,CtoPstr("Now computing noise ..."),TRUE,zoomDocProc,(WindowPtr) -1L,TRUE,0L);
  119.     myPixMapHandle = ((CGrafPtr)window)->portPixMap;
  120.     MoveHHi((Handle) myPixMapHandle);
  121.     HLock((Handle) myPixMapHandle);
  122.  
  123.     /* Compute noise */
  124.     /* allocate buffer BitMap */
  125.     buffer.rowBytes=1024;
  126.     MaxApplZone();
  127.     bufferBytes = FreeMem();
  128.     bufferBytes-=100000L;    /* Grab most of available space. */
  129.     bufferBytes -= bufferBytes%buffer.rowBytes;
  130.     buffer.baseAddr=NULL;
  131.     for(;bufferBytes>0;bufferBytes -= buffer.rowBytes){
  132.         buffer.baseAddr = NewPtr(bufferBytes);
  133.         if (buffer.baseAddr!=NULL)break;
  134.     }
  135.     if(buffer.baseAddr==NULL)PrintfExit("\007Sorry, not enough memory for buffers.\n");
  136.     SetRect(&buffer.bounds,0,0,buffer.rowBytes*8L,bufferBytes/buffer.rowBytes);
  137.  
  138.     /* Fill buffer with noise. */
  139.     bufferPtr = (short *) buffer.baseAddr;
  140.     RandFill(buffer.baseAddr,bufferBytes);
  141.  
  142.     /* Show noise */
  143.     SetPort(window);
  144.     reSize=0;
  145.     while (1) {
  146.         wStateData=(*((WStateData **)((WindowPeek)window)->dataHandle));
  147.         unzoomedRect=wStateData->userState;
  148.         zoomedOut=ZoomedOut(window);
  149.         if(reSize){
  150.             SizeWindow(window,LoWord(reSize),HiWord(reSize),TRUE);
  151.             reSize=0L;
  152.         }
  153.         device=GetWindowDevice(window);
  154.         if(device==NULL){
  155.             done=1;
  156.             break;
  157.         }
  158.         r=window->portRect;
  159.         LocalToGlobalRect(&r);
  160.         gdRect=(*device)->gdRect;
  161.         /* leave room for window's title bar */
  162.         gdRect.top+=r.top-1-(*((WindowPeek)window)->strucRgn)->rgnBBox.top;
  163.         /* leave room for menu bar */
  164.         if(device==GetMainDevice())gdRect.top+=MBarHeight;
  165.         SectRect(&gdRect,&r,&r);
  166.         pixelSize=(**(**device).gdPMap).pixelSize;
  167.         i=r.left-(*device)->gdRect.left;
  168.         i=(i*pixelSize+16 & ~31)/pixelSize-i;    /* move to nearest 32-bit boundary */
  169.         OffsetRect(&r,i,0);                        /* Assumes window is >16 bits wide */
  170.         SectRect(&gdRect,&r,&r);
  171.         MoveWindow(window,r.left,r.top,TRUE);
  172.         OffsetRect(&r,-r.left,-r.top);
  173.         sand = **(*device)->gdPMap;    /* Initialize PixMap fields from screen */
  174.         sand.bounds=r;
  175.         sand.bounds.right  -=SCROLL_BAR;    /* Leave room for the scroll bars */
  176.         sand.bounds.bottom -=SCROLL_BAR;
  177.         sand.bounds.right=(sand.bounds.right*sand.pixelSize+14 & ~31)/sand.pixelSize;
  178.             /* round to multiple of 32 bits */
  179.         if(sand.bounds.right<32)sand.bounds.right=32;
  180.         sand.rowBytes = (sand.bounds.right*sand.pixelSize + 31 & ~31)/8;
  181.             /* round up to multiple of 32 bits (redundant) */
  182.         sandBytes = (size_t) sand.rowBytes * (size_t) sand.bounds.bottom;
  183.         if(bufferBytes/2 < sandBytes)
  184.         {
  185.             /* restrict height by amount of noise available */
  186.             sand.bounds.bottom=bufferBytes/sand.rowBytes/2;
  187.             sandBytes = (size_t) sand.rowBytes * (size_t) sand.bounds.bottom;
  188.         }    
  189.         sand.rowBytes |= 0x8000;    /* Mark it as a PixMap */
  190.         sand.pmVersion=0;
  191.         sand.packType=0;
  192.         sand.packSize=0;
  193.         sand.planeBytes=0;
  194.         sand.pmReserved=0;
  195.         SizeWindow(window
  196.             ,sand.bounds.right+SCROLL_BAR,sand.bounds.bottom+SCROLL_BAR,TRUE);
  197.         EraseRect(&window->portRect);
  198.         DrawGrowIcon(window);
  199.         if(zoomedOut && windowEvent!=inGrow && windowEvent!=inDrag){
  200.             wStateData=(*((WStateData **)((WindowPeek)window)->dataHandle));
  201.             r=window->portRect;
  202.             LocalToGlobalRect(&r);
  203.             r.left-=1;
  204.             r.right-=2;
  205.             wStateData->stdState=r;        
  206.             wStateData->userState=unzoomedRect;
  207.         }
  208.         sprintf(string,"%dx%dx%d bits"
  209.             ,sand.bounds.right,sand.bounds.bottom,sand.pixelSize);
  210.         SetWTitle(window,CtoPstr(string));
  211.         FlushEvents(everyEvent,0);
  212.         DiffRgn(GetGrayRgn(),((WindowPeek)window)->strucRgn,ignoreRgn);
  213.         while (1) {
  214.             update=0;            /* don't update window frame unless necessary */
  215.             #if RAISE_PRIORITY
  216.                 SetPriority(2);    /* Get more speed by suppressing keyboard, mouse, and ticks. */
  217.             #endif
  218.             sand.baseAddr = buffer.baseAddr;
  219.             /* choose grain that won't overflow short argument to nrand() */
  220.             i=(bufferBytes/SHRT_MAX+3)&~3;    /* round up to multiple of 4 */
  221.             sand.baseAddr += nrand((bufferBytes-sandBytes)/i)*(long)i;
  222.             if(((WindowPeek)window)->hilited){
  223.                 CopyBitsQuickly((BitMap *)&sand,(BitMap *)*myPixMapHandle
  224.                     ,&sand.bounds,&sand.bounds,srcCopy,NULL);
  225.             }
  226.             #if RAISE_PRIORITY
  227.                 SetPriority(0);    /* Revive keyboard and mouse. */
  228.             #endif
  229.             #if WAIT_NEXT_EVENT
  230.                if(WaitNextEvent(mDownMask+keyDownMask,&theEvent,0,ignoreRgn)){
  231.                #else
  232.                if(GetNextEvent(mDownMask+keyDownMask,&theEvent)){
  233.                #endif
  234.                 switch(theEvent.what){
  235.                 case keyDown:
  236.                     if(theEvent.modifiers & cmdKey) switch(theEvent.message & charCodeMask) {
  237.                         case '.':
  238.                         case 'w':
  239.                         done=1;
  240.                     }
  241.                     break;
  242.                 case mouseDown:
  243.                     update=1;
  244.                     windowEvent=FindWindow(theEvent.where,&aWindow);
  245.                     if(aWindow==window){
  246.                         switch(windowEvent){
  247.                         case inDrag:
  248.                             DragWindow(window,theEvent.where,&qd.screenBits.bounds);
  249.                             break;
  250.                         case inGoAway:
  251.                             done=TrackGoAway(window,theEvent.where);
  252.                             break;
  253.                         case inGrow:
  254.                             r.top=r.left=32+SCROLL_BAR;    /* minimum size */
  255.                             r.bottom=r.right=1024+SCROLL_BAR;    /* max size */
  256.                             reSize = GrowWindow(window,theEvent.where,&r);
  257.                             break;
  258.                         case inZoomIn:
  259.                         case inZoomOut:
  260.                             Zoom(window,windowEvent);
  261.                             break;
  262.                         case inContent:
  263.                             break;
  264.                         default:
  265.                             update=0;
  266.                             break;
  267.                         }
  268.                         break;
  269.                     }
  270.                     break;
  271.                 }
  272.             }            
  273.             if(update || done)break;
  274.         }
  275.         if(done)break;
  276.     }
  277.     SetPort(oldPort);
  278.     DisposeRgn(ignoreRgn);
  279.     DisposPtr(buffer.baseAddr);
  280.     DisposeWindow(window);
  281. }
  282.  
  283. Boolean ZoomedOut(WindowPtr w)
  284. /* Determine whether window is "standard" size, i.e. zoomed out. */
  285. {
  286.     Point pt;
  287.     Rect r,myRect;
  288.     Boolean zoomedOut;
  289.     
  290.     myRect=(*((WStateData **)((WindowPeek)w)->dataHandle))->stdState;
  291.     pt.h=0;
  292.     pt.v=0;
  293.     LocalToGlobal(&pt);
  294.     r = w->portRect;
  295.     OffsetRect(&r,pt.h,pt.v);
  296.     zoomedOut=r.top==myRect.top
  297.         && r.left==myRect.left
  298.         && r.bottom==myRect.bottom
  299.         && r.right==myRect.right;
  300.     return zoomedOut;
  301. }